package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.GoodsBase;
import com.caocaog.market.entity.dto.GoodsDTO;
import com.caocaog.market.entity.vo.GoodsVO;
import com.caocaog.market.service.GoodsBaseService;
import com.caocaog.market.service.GoodsService;
import com.caocaog.market.service.ShopGenericService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.Trim;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.base.entity.groups.UpdateGroup;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 商品基础信息
 *
 * @author chechen
 * @date 2022-04-18
 */
@Slf4j
@RestController
@Api(value = "base", tags = GoodsBaseController.TABLE_MEANING)
@RequestMapping("/goods/goodsBasic")
public class GoodsBaseController extends GenericController<GoodsBase, GoodsBaseService> {
    public static final String TABLE_MEANING = "商品基础信息";
    private GoodsService goodsService;
    private ShopGenericService shopGenericService;

    @ApiOperation(value = PAGE)
    @GetMapping(value = "/list")
    public Result<IPage<GoodsBase>> queryPageList(GoodsBase goodsBase, QueryRequest queryRequest) {
        if (StringUtils.isBlank(goodsBase.getShopId())) {
            List<String> shopIds = this.shopGenericService.getShopIds();
            if (shopIds == null)
                throw new JeecgBootException("未找到门店");
            goodsBase.setShopId(StringUtils.join(shopIds, StringPool.COMMA));
        }

        Page<GoodsBase> page = PageGenerator.createPageSort(queryRequest);
        return Result.OK(this.service.queryGoodsBasePage(page, goodsBase));
    }

    @Trim
    @AutoLog(TABLE_MEANING + ADD)
    @ApiOperation(value = ADD, notes = "管理员角色设置默认shopId-> " + CommonConstant.DEFAULT_SHOP_ID)
    @PostMapping(value = "/add")
    public Result<GoodsVO> add(@RequestBody @Valid GoodsDTO goods) {
        goods.setSorts("1744616543329120257");
        if (StringUtils.isBlank(goods.getShopId())) {
            log.info("处理商品信息的shopId");
            this.shopGenericService.dealWithShopId(GoodsDTO::setShopId, goods);
        }
        log.info(TABLE_MEANING + ADD);
        return Result.OK(ADD_SUCCESS, this.goodsService.addGoods(goods));
    }

    @AutoLog(TABLE_MEANING + EDIT)
    @ApiOperation(value = EDIT, notes = "管理员角色设置默认shopId-> " + CommonConstant.DEFAULT_SHOP_ID)
    @PutMapping(value = "/edit")
    public Result<GoodsVO> edit(@RequestBody @Validated({UpdateGroup.class}) GoodsDTO goods) {
        log.info(TABLE_MEANING + EDIT);
        return Result.OK(EDIT_SUCCESS, this.goodsService.editGoods(goods));
    }

    @AutoLog(TABLE_MEANING + DELETE)
    @ApiOperation(DELETE)
    @DeleteMapping(value = "/delete")
    public Result<Integer> delete(@RequestParam String id) {
        log.info(TABLE_MEANING + DELETE);
        return Result.OK(DELETE_SUCCESS, this.goodsService.deleteGoods(id));
    }

    @AutoLog(TABLE_MEANING + DELETE_BATCH)
    @ApiOperation(DELETE_BATCH)
    @DeleteMapping(value = "/deleteBatch")
    public Result<Integer> deleteBatch(@RequestParam String ids) {
        return Result.OK(DELETE_SUCCESS, this.goodsService.deleteGoods(ids.split(StringPool.COMMA)));
    }

    /**
     * 将商品 id 进行上下架切换
     * @param id 商品ID
     * @return 执行结果
     */
    @AutoLog("切换上下架状态")
    @ApiOperation("切换上下架状态")
    @GetMapping(value = "/statusChange/{id}")
    public Result<Integer> statusChange(@PathVariable String id) {
        log.info("切换上下架状态");
        return Result.OK(OPERATE_SUCCESS, super.service.statusChange(id));
    }

    /**
     * 将商品 id 是否在首页四格展示进行切换
     * @param id 商品ID
     * @return 执行结果
     */
    @AutoLog("切换是否是在首页四格展示")
    @ApiOperation("切换是否上首页状态")
    @GetMapping(value = "/isIndexChange/{id}")
    public Result<Integer> isIndexChange(@PathVariable String id) {
        log.info("切换是否上首页状态");
        return Result.OK(OPERATE_SUCCESS, super.service.isIndexChange(id));
    }

    /**
     * 将商品 id 进行热门与非热门进行切换
     * @param id 商品ID
     * @return 执行结果
     */
    @AutoLog("切换是否是热门状态")
    @ApiOperation("切换是否是热门状态")
    @GetMapping(value = "/isHotChange/{id}")
    public Result<Integer> isHotChange(@PathVariable String id) {
        log.info("切换是否是热门状态");
        return Result.OK(OPERATE_SUCCESS, super.service.isHotChange(id));
    }

    @AutoLog("切换是单点不送状态")
    @ApiOperation("切换单点不送状态")
    @GetMapping(value = "/oneNoDeliveryChange/{id}")
    public Result<Integer> oneNoDeliveryChange(@PathVariable String id) {
        log.info("切换单点不送状态");
        return Result.OK(OPERATE_SUCCESS, super.service.oneNoDeliveryChange(id));
    }

    @ApiOperation(GET_BY_ID)
    @GetMapping(value = "/queryById")
    public Result<GoodsBase> queryById(@RequestParam String id) {
        return Result.OK(super.service.getByIdCache(id));
    }


    @Autowired
    public void setGoodsService(GoodsService goodsService) {
        this.goodsService = goodsService;
    }
    @Autowired
    public void setShopGenericService(ShopGenericService shopGenericService) {
        this.shopGenericService = shopGenericService;
    }
}
